package com.google.coffeeshop.gui.desktop.orderpayment;

import java.util.List;

import javax.swing.table.DefaultTableModel;

import com.google.coffeeshop.entity.OrderLineDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class ItemTable extends AbstractBaseTable {
	private static final long serialVersionUID = 1L;
	
	private static String COL_STATUS = "Status";
	private static String COL_ITEM_NAME = "Item Name";
	private static String COL_QUANTITY = "Qty";
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_STATUS, COL_ITEM_NAME, COL_QUANTITY
		};
	
	private DefaultTableModel model = new DefaultTableModel(
			rowData,
			columnNames
		);
	
	public ItemTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
//		adjustColumnSizes();
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	
	private void adjustColumnSizes() {
		findColumn(model, COL_STATUS).setPreferredWidth(30);
		findColumn(model, COL_QUANTITY).setPreferredWidth(5);
	}
	
	public void removeAllRow() {
		model.setRowCount(0);
	}
	
	private void addNewRow(OrderLineDTO orderLine) {
		addNewRow(orderLine.isServed() ? "Served" : "Waiting", orderLine.getProduct().getName(), orderLine.getQuantity());
	}
	
	private void addNewRow(String status, String name, long quantity) {
		Object[] objects = new Object[] {status, name, quantity};
		model.addRow(objects);
	}

	public void addNewRows(List<OrderLineDTO> orderLines) {
		for (OrderLineDTO orderLine : orderLines) {
			addNewRow(orderLine);
		}
//		arrageOrderLineNumbers();
	}

	private void arrageOrderLineNumbers() {
		for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
			model.setValueAt(rowIndex + 1, rowIndex, 1);
		}
	}
	
}
